package cn.hdu.community.service;

import cn.hdu.community.exception.GlobalException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.mail.SimpleMailMessage;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

/**
 * @Author zhangxin
 * @Date 2022/4/1
 */
@Service("mailService")
public class MailService {
    private static final Logger logger = LoggerFactory.getLogger(GlobalException.class);
    @Value("${spring.mail.username}")
    private String from;
    @Value("${spring.mail.nickname}")
    private String nickname;

    @Resource
    private JavaMailSender mailSender;

    /**
     * 发送邮箱
     *
     * @param to
     * @param title
     * @param content
     */
    public void sendMail(String to, String title, String content) {
        SimpleMailMessage message = new SimpleMailMessage();
        message.setFrom(nickname + '<' + from + '>');
        message.setTo(to);
        message.setSubject(title);
        message.setText(content);
        mailSender.send(message);
        logger.info("邮件发送成功");
    }
}